package com.example.flow.biz.interceptor.approval;


import com.example.flow.constant.FlowConstant;
import com.example.flow.dao.FlowApproveApplyDao;
import com.example.flow.dao.model.FlowApproveApply;
import com.example.flow.enums.EnumReply;
import com.example.flow.flow.FlowNodeInterceptor;
import com.example.flow.flow.core.Execution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 非进件节点默认拦截器
 *
 * @author safe
 * @date 2021/1/30
 */
@Service
public class DefaultCommonApprovalInterceptor implements FlowNodeInterceptor {
    private static Logger log = LoggerFactory.getLogger(DefaultCommonApprovalInterceptor.class);

    @Autowired
    private FlowApproveApplyDao approveApplyDao;

    @Override
    public void intercept(Execution execution) {
        String replyCode = (String) execution.getArgs()
                .get(FlowConstant.REPLY_RESULT);
        EnumReply reply = EnumReply.find(replyCode);
        log.info("任务[{}]进入DefaultCommonApprovalInterceptor,结果[{}]", execution.getOrder()
                .getId(), reply);
        if (null == reply) {
            log.error("审批结果为空replyCode:{}", replyCode);
            throw new RuntimeException("审批结果为空");
        }
        // todo 插入历史

        FlowApproveApply approveApply = approveApplyDao.queryOne(null, execution.getOrder()
                .getOrderId());

        approveApply.setCreateOpetatorId(null);
        approveApply.setCurrentTaskId(null);
        approveApply.setCurrentNodeCode(null);
        approveApply.setCurrentNodeName(null);
        approveApply.setUpdateTime(new Date());
        approveApplyDao.updateByExample(approveApply);
    }
}
